/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main.java
 *
 * Created on Aug 14, 2010, 6:29:36 AM
 */
package GUI;

import BusinessLogic.Login_BL;
import BusinessLogic.User;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

//Class Display to set Banner Image
/**
 *
 * @author thangvv0907i
 */
public class Main extends javax.swing.JFrame {

    private Dimension ds;
    private int ws;
    private int hs;
    private int test;
    private User userObj;
    private String uid;

    /** Creates new form Main */
    public Main(User obj) {
        userObj = obj;
        try {
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        ds = Toolkit.getDefaultToolkit().getScreenSize();
        ws = ds.width;
        hs = ds.height;


        test = userObj.getPermiss();
        initComponents();
        lblHello.setText(userObj.getName());
        uid = userObj.getUserID();
        if (userObj.getLastLogin() != null) {
            lblLastLogin.setText(userObj.getLastLogin());
        } else {
            lblLastLogin.setText("First Login");
        }

        setSize(1024, 768 - 15);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnBanner = new javax.swing.JPanel();
        pnLeft = new javax.swing.JPanel();
        pnRight = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblHello = new javax.swing.JLabel();
        lblLastLogin = new javax.swing.JLabel();
        mnBar = new javax.swing.JMenuBar();
        mnSystem = new javax.swing.JMenu();
        mnLogout = new javax.swing.JMenuItem();
        mnExit = new javax.swing.JMenuItem();
        mnHelp = new javax.swing.JMenu();
        mnAboutMe = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Inventory Management System");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        pnBanner.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnBanner.setPreferredSize(new java.awt.Dimension(1024, 135));

        pnBanner = new DisplayImage();

        javax.swing.GroupLayout pnBannerLayout = new javax.swing.GroupLayout(pnBanner);
        pnBanner.setLayout(pnBannerLayout);
        pnBannerLayout.setHorizontalGroup(
            pnBannerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1020, Short.MAX_VALUE)
        );
        pnBannerLayout.setVerticalGroup(
            pnBannerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 131, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout pnLeftLayout = new javax.swing.GroupLayout(pnLeft);
        pnLeft.setLayout(pnLeftLayout);
        pnLeftLayout.setHorizontalGroup(
            pnLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 785, Short.MAX_VALUE)
        );
        pnLeftLayout.setVerticalGroup(
            pnLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 552, Short.MAX_VALUE)
        );

        if(test==4)
        {
            pnLeft = new Admin(userObj);
        }else if(test==1){
            pnLeft = new InventoryManager(userObj);
        }else if(test == 2){
            pnLeft = new SalesManagers(userObj);
        }else {
            pnLeft = new SalesMen(userObj);
        }

        pnRight.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Hello :");

        jLabel2.setText("Last Login: ");

        lblHello.setFont(new java.awt.Font("Tahoma", 1, 11));

        javax.swing.GroupLayout pnRightLayout = new javax.swing.GroupLayout(pnRight);
        pnRight.setLayout(pnRightLayout);
        pnRightLayout.setHorizontalGroup(
            pnRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnRightLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblLastLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .addComponent(lblHello, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnRightLayout.setVerticalGroup(
            pnRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnRightLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(lblHello, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
                .addGap(11, 11, 11)
                .addGroup(pnRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblLastLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(484, 484, 484))
        );

        mnBar.setBackground(new java.awt.Color(123, 190, 190));

        mnSystem.setMnemonic('s');
        mnSystem.setBackground(new java.awt.Color(123, 190, 190));
        mnSystem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/System.png"))); // NOI18N
        mnSystem.setText("System");
        mnSystem.setFont(new java.awt.Font("Tahoma", 0, 12));
        mnSystem.setMaximumSize(new java.awt.Dimension(87, 32767));

        mnLogout.setMnemonic('l');
        mnLogout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.ALT_MASK));
        mnLogout.setBackground(new java.awt.Color(246, 241, 241));
        mnLogout.setFont(new java.awt.Font("Arial", 0, 12));
        mnLogout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/logout.png"))); // NOI18N
        mnLogout.setText("Logout");
        mnLogout.setMargin(new java.awt.Insets(2, -10, 2, 2));
        mnLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnLogoutActionPerformed(evt);
            }
        });
        mnSystem.add(mnLogout);
        mnSystem.addSeparator();

        mnExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.ALT_MASK));
        mnExit.setBackground(new java.awt.Color(246, 241, 241));
        mnExit.setFont(new java.awt.Font("Arial", 0, 12));
        mnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/exit.png"))); // NOI18N
        mnExit.setText("Exit");
        mnExit.setIconTextGap(0);
        mnExit.setMargin(new java.awt.Insets(2, -15, 2, 2));
        mnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnExitActionPerformed(evt);
            }
        });
        mnSystem.add(mnExit);

        mnBar.add(mnSystem);

        mnHelp.setBackground(new java.awt.Color(123, 190, 190));
        mnHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Icon24-Help.png"))); // NOI18N
        mnHelp.setText("Help");
        mnHelp.setFont(new java.awt.Font("Tahoma", 0, 12));

        mnAboutMe.setBackground(new java.awt.Color(246, 241, 241));
        mnAboutMe.setFont(new java.awt.Font("Arial", 0, 12));
        mnAboutMe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/about.png"))); // NOI18N
        mnAboutMe.setText("Get Help");
        mnAboutMe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnAboutMeActionPerformed(evt);
            }
        });
        mnHelp.add(mnAboutMe);

        mnBar.add(mnHelp);

        setJMenuBar(mnBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnBanner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnLeft, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnRight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnBanner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnRight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnLeft, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnExitActionPerformed
        // TODO add your handling code here:
        new Login_BL().setLastLogin_BL(userObj.getUserID(), "dd/MM/yyyy");
        checkExit();
    }//GEN-LAST:event_mnExitActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        new Login_BL().setLastLogin_BL(userObj.getUserID(), "dd/MM/yyyy");
        checkExit();
    }//GEN-LAST:event_formWindowClosing

    private void mnLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnLogoutActionPerformed
        // TODO add your handling code here:
        new Login_BL().setLastLogin_BL(userObj.getUserID(), "dd/MM/yyyy");
        this.dispose();
        new LoginForm().setVisible(true);
    }//GEN-LAST:event_mnLogoutActionPerformed

    private void mnAboutMeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnAboutMeActionPerformed
        // TODO add your handling code here:
        File f = new File("C:\\UserGuids.pdf");
        try {
            /*HelpManger help=new HelpManger();
            help.setVisible(true);*/
            Process p = Runtime.getRuntime().exec("rundll32.exe url.dll,FileProtocolHandler "+f.getAbsolutePath());
            p.waitFor();
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_mnAboutMeActionPerformed


    //Method check Exit event
    public void checkExit() {
        int check = JOptionPane.showConfirmDialog(this, "Do you want to Exit?", "Exit", JOptionPane.OK_CANCEL_OPTION);
        if (check == 0) {
            System.exit(0);
        }
    }
    //Method set Permisson
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblHello;
    private javax.swing.JLabel lblLastLogin;
    private javax.swing.JMenuItem mnAboutMe;
    private javax.swing.JMenuBar mnBar;
    private javax.swing.JMenuItem mnExit;
    private javax.swing.JMenu mnHelp;
    private javax.swing.JMenuItem mnLogout;
    private javax.swing.JMenu mnSystem;
    private javax.swing.JPanel pnBanner;
    private javax.swing.JPanel pnLeft;
    private javax.swing.JPanel pnRight;
    // End of variables declaration//GEN-END:variables
}

//Class set Image for Banner
class DisplayImage extends JPanel {

    Image castle;
    Dimension size;

    public DisplayImage() {
        size = new Dimension();

        ImageIcon picIcon = new ImageIcon(getClass().getResource("/Images/bannermain.jpg"));
        castle = picIcon.getImage();

        size.width = castle.getWidth(null);
        size.height = castle.getHeight(null);
        setPreferredSize(size);
        setBackground(new java.awt.Color(153, 204, 255));

    }

    @Override
    public void paint(Graphics g) {


        Graphics2D g2d = (Graphics2D) g;

        g2d.drawImage(castle, 0, 0, null);
    }
}
